package Persistance::LifeAssistantDao;

use strict;
use warnings;
use 5.010;

use Utils::DbUtils qw(query_for_array update);
use Pojo::Housework;
use Pojo::BloodSugar;
use Pojo::LocationTrack;
use Pojo::Contacts;
use Utils::Constants;
use Exporter qw(import);

sub get_housework_data {
	my ($user) = @_;
	my @params = ();
	my @res = ();
	my $sql = '';
	
	if (defined $user) {
		$sql = Utils::Constants::GET_HOUSEWORK_SQL;
		push @params, $user;
	}
	else {
		$sql = Utils::Constants::GET_ALL_HOUSEWORK_SQL;
	}

	@res = query_for_array($sql, "Pojo::Housework", @params);

	return @res;
}

sub get_specified_housework_detail {
	my ($housework_id) = @_;
	my @res = query_for_array(Utils::Constants::GET_SPECIFIED_HOUSEWORK_SQL, "Pojo::Housework", $housework_id);

	return @res;
}

sub insert_new_housework {
	my ($user_id,$task,$reward) = @_;
	my $rows_affected = update(Utils::Constants::INSERT_NEW_HOUSEWORK_SQL, $task, $reward, $user_id, Utils::Constants::HOUSEWORK_STATUS_DOING);
	
	return $rows_affected;
}

sub update_housework_status {
    my ($housework_id) = @_;
    my $rows_affected = update(Utils::Constants::UPDATE_HOUSEWORK_STATUS_SQL, $housework_id);

    return $rows_affected;
}

sub get_bloodsugar_history_list {
	my ($user_id) = @_;
	my @res = query_for_array(Utils::Constants::GET_BLOODSUGAR_HISTORY_LIST_SQL, "Pojo::BloodSugar", $user_id);

	return @res;
}

sub insert_new_bloodsugar {
	my ($user_id,$date,$time,$tag,$value) = @_;
	my $rows_affected = update(Utils::Constants::INSERT_NEW_BLOODSUGAR_SQL, $date, $time, $tag, $value, $user_id);

	return $rows_affected;
}

sub insert_new_location_track {
	my ($location_desc,$user_id) = @_;
	my $rows_affected = update(Utils::Constants::INSERT_NEW_LOCATION_SQL,$location_desc,$user_id);

	return $rows_affected;
}

sub get_location_track_by_user {
	my ($user_id) = @_;
	my @res = query_for_array(Utils::Constants::GET_LOCATION_TRACK_FOR_USER_SQL, "Pojo::LocationTrack", $user_id);

	return @res;
}

sub get_all_users_location_in_current_day {
    my @res = query_for_array(Utils::Constants::GET_ALL_USERS_LOCATION_TODAY_SQL, "Pojo::LocationTrack");

    return @res;
}

sub insert_or_update_contacts {
    my ($user_id, $contacts) = @_;
    my $rows_affected = update(Utils::Constants::INSERT_OR_UPDATE_CONTACTS_BY_USER_ID_SQL, $user_id, $contacts);

    return $rows_affected;
}

sub get_contacts_by_user_id {
    my $user_id = shift;
    my @res = query_for_array(Utils::Constants::GET_CONTACTS_BY_USER_ID_SQL, "Pojo::Contacts", $user_id);

    return @res;
}

1;
